<?php

namespace app\common\library;

use app\common\model\User;
use think\exception\DbException;

class YuAccount
{
    const VERSION = '6.0.5';

    protected $errorString = '';

    public static $instance ;

    /**
     * @title 单例调用
     * @description 单例调用
     * @author Admin ldy
     * @time 2018/9/25  12:22
     */
    public static function getInstance(){
        if (YuAccount::$instance == null){
            YuAccount::$instance = new \app\common\library\YuAccount;
            return YuAccount::$instance;
        }else{
            return YuAccount::$instance;
        }
    }

    public function __construct(){
        //初始化
    }

    public function setErrorString($msg){
        $this->errorString = $msg;
    }

    public function getErrorString(){
        return $this->errorString;
    }

    /**
     * @title 给用户加一个积分
     * @description 给用户加一个积分
     * @author Admin ldy
     * @param name:$uid  type:string require:1 default:1 desc:
     * @param name:$score  type:string require:1 default:1 desc:
     * @time 2018/10/16  9:29
     * @return ReFail|ReSuccess
     */
    public static function addScore($uid,$score){
        $where = array(
            'id'=>$uid
        );
        try{
            $user = User::where($where)->find();
            $user->score = $user->score + $score;
            $re = $user->save();
            if ($re){
                return new ReSuccess('数据处理成功！',$user);
            }else{
                return new ReFail(ErrorType::ACTION_FAILED);
            }
        }catch (Exception $e){
            return new ReFail(ErrorType::DATA_EXCEPTION,$e->getTraceAsString());
        }catch (DbException $dbe){
            return new ReFail(ErrorType::DATA_EXCEPTION,$dbe->getTraceAsString());
        }
    }

    /**
     * @title 给用户加一个经验
     * @description 给用户加一个经验
     * @author Admin ldy
     * @param name:$uid  type:string require:1 default:1 desc:用户id
     * @param name:$experience  type:string require:1 default:1 desc:用户的经验
     * @time 2018/10/16  9:32
     * @return ReFail|ReSuccess
     */
    public static function addExperience($uid,$experience){
        $where = array(
            'id'=>$uid
        );
        try{
            $user = User::where($where)->find();
            $user->experience = $user->experience + $experience;
            $re = $user->save();
            if ($re){
                return new ReSuccess('数据处理成功！',$user);
            }else{
                return new ReFail(ErrorType::ACTION_FAILED);
            }
        }catch (Exception $e){
            return new ReFail(ErrorType::DATA_EXCEPTION,$e->getTraceAsString());
        }catch (DbException $dbe){
            return new ReFail(ErrorType::DATA_EXCEPTION,$dbe->getTraceAsString());
        }
    }

    /**
     * @title 根据email获得用户id
     * @description 根据email获得用户id
     * @author Admin ldy
     * @param name:email  type:string require:1 default:1 desc:
     * @time 2018/10/22  17:28
     * @return ReFail|ReSuccess
     */
    public static function getUidByEmail($email){
        $where = array(
            'email'=>$email
        );
        try{
            $user = User::where($where)->field('id')->find();
            if ($user){
                return new ReSuccess('数据处理成功！',$user);
            }else{
                return new ReFail(ErrorType::ACTION_FAILED);
            }
        }catch (Exception $e){
            return new ReFail(ErrorType::DATA_EXCEPTION,$e->getTraceAsString());
        }catch (DbException $dbe){
            return new ReFail(ErrorType::DATA_EXCEPTION,$dbe->getTraceAsString());
        }
    }

    /**
     * @title 获取用户的积分
     * @description 获取用户的积分
     * @author Admin ldy
     * @param name:$uid  type:string require:1 default:1 desc:
     * @time 2018/10/24  16:37
     * @return ReFail|ReSuccess
     */
    public static function getScoreByUserId($uid){
        $where = array(
            'id'=>$uid
        );
        try{
            $user = User::where($where)->field('score')->find();
            if ($user){
                return new ReSuccess('数据查询成功！',$user);
            }else{
                return new ReFail(ErrorType::ACTION_FAILED);
            }
        }catch (Exception $e){
            return new ReFail(ErrorType::DATA_EXCEPTION,$e->getTraceAsString());
        }catch (DbException $dbe){
            return new ReFail(ErrorType::DATA_EXCEPTION,$dbe->getTraceAsString());
        }
    }

    /**
     * @title 获取用户的经验值
     * @description 获取用户的经验值
     * @author Admin ldy
     * @param name:$uid  type:string require:1 default:1 desc:
     * @time 2018/10/24  16:37
     * @return ReFail|ReSuccess
     */
    public static function getExpByUserId($uid){
        $where = array(
            'id'=>$uid
        );
        try{
            $user = User::where($where)->field('experience')->find();
            if ($user){
                return new ReSuccess('数据查询成功！',$user);
            }else{
                return new ReFail(ErrorType::ACTION_FAILED);
            }
        }catch (Exception $e){
            return new ReFail(ErrorType::DATA_EXCEPTION,$e->getTraceAsString());
        }catch (DbException $dbe){
            return new ReFail(ErrorType::DATA_EXCEPTION,$dbe->getTraceAsString());
        }
    }

    /**
     * @title 根据用户的微信昵称获取用户的id
     * @description 根据用户的微信昵称获取用户的id
     * @author Admin ldy
     * @param name:$wxNickNmae  type:string require:1 default:1 desc:
     * @time 2018/10/31  11:35
     * @return ReFail|ReSuccess
     */
    public static function getUidByWxNickName($wxNickNmae){
        $where = array(
            'wx_nickname'=>$wxNickNmae
        );
        try{
            $user = User::where($where)->field('id')->find();
            if ($user){
                return new ReSuccess('数据查询成功！',$user);
            }else{
                return new ReFail(ErrorType::ACTION_FAILED);
            }
        }catch (Exception $e){
            return new ReFail(ErrorType::DATA_EXCEPTION,$e->getTraceAsString());
        }catch (DbException $dbe){
            return new ReFail(ErrorType::DATA_EXCEPTION,$dbe->getTraceAsString());
        }
    }





}
